package com.itheima.day05.contact.dao;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.itheima.day05.contact.pojo.Contact;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Repository;

public class ContactRepository {

    private static final  URL url = ContactRepository.class.getClassLoader().getResource("contacts.txt");


    public void save(Contact contact) {
        try {
            String line = contact.getId() + "," + contact.getName() + "," + contact.getPhone();
            FileUtils.writeStringToFile(new File(url.toURI()), line + System.lineSeparator(), true);
        } catch (Exception e) {
            throw new RuntimeException("Failed to save contact", e);
        }
    }

    public List<Contact> findAll() {
        List<Contact> contacts = new ArrayList<>();
        try {
            List<String> lines = FileUtils.readLines(new File(url.toURI()));
            for (String line : lines) {
                String[] parts = line.split(",");
                Contact contact = new Contact(Integer.parseInt(parts[0]), parts[1], parts[2]);
                contacts.add(contact);
            }
        } catch (Exception e) {
            System.out.println("查询报错信息.............");
            System.out.println(e.getMessage());
            throw new RuntimeException("Failed to find all contacts", e);
        }
        return contacts;
    }

    public Contact findById(Integer  id) {
        try {
            List<String> lines = FileUtils.readLines(new File(url.toURI()),Charset.forName("GBK"));
            for (String line : lines) {
                String[] parts = line.split(",");
                if (Integer.parseInt(parts[0])== id) {
                    return new Contact(Integer.parseInt(parts[0]), parts[1], parts[2]);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to find contact by id", e);
        }
        return null;
    }

    public void update(Contact contact) {
        List<Contact> contacts = findAll();
        try {
            StringBuilder sb = new StringBuilder();
            for (Contact c : contacts) {
                if (c.getId().equals(contact.getId())) {
                    sb.append(contact.getId()).append(",").append(contact.getName()).append(",").append(contact.getPhone()).append(System.lineSeparator());
                } else {
                    sb.append(c.getId()).append(",").append(c.getName()).append(",").append(c.getPhone()).append(System.lineSeparator());
                }
            }
            FileUtils.writeStringToFile(new File(url.toURI()), sb.toString(), false);
        } catch (Exception e) {
            throw new RuntimeException("Failed to update contact", e);
        }
    }
    public void delete(Integer id) {
        List<Contact> contacts = findAll();
        try {
            StringBuilder sb = new StringBuilder();
            for (Contact c : contacts) {
                if (!c.getId().equals(id)) {
                    sb.append(c.getId()).append(",").append(c.getName()).append(",").append(c.getPhone()).append(System.lineSeparator());
                }
            }
            FileUtils.writeStringToFile(new File(url.toURI()), sb.toString(), false);
        } catch (Exception e) {
            throw new RuntimeException("Failed to delete contact", e);
        }
    }
}